const mysql = require('mysql')
const { 
    MYSQL_HOST, 
    MYSQL_PORT, 
    MYSQL_DATABASE, 
    MYSQL_USER,
    MYSQL_PASSWORD 
} = require('../config/config.default')

// 连接池
const pool = mysql.createPool({
    host: MYSQL_HOST,
    port: MYSQL_PORT,
    database: MYSQL_DATABASE,
    user: MYSQL_USER,
    password: MYSQL_PASSWORD
})

console.log('pool:', pool)

// 对数据库进行增删改查的基础
function query(sql, callback) {
    pool.getConnection(function(err,connection){
        connection.query(sql, function(err,rows){
            callback(err,rows)
            connection.release() // 中断连接
        })
    })
}

exports.query = query;